import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe
import os

def aesthetics_net():
    plt.rcParams['figure.figsize'] = (10,10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'

    # caffe_root = 'C:/Program Files/caffe/caffe-master/caffe-master'
    # sys.path.insert(0, caffe_root+'python')
    caffe.set_mode_cpu()
    Home=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    root=str(Home).replace('\\','/')+'/mysrc/'
    model_def = root + 'evaluation/deploy.prototxt'
    model_weights = root +  'evaluation/ILGnet-AVA2.caffemodel'
    mean_filename = root +  'evaluation/AVA2_mean.npy'

    net = caffe.Net(model_def, model_weights, caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2,0,1))  
    # transformer.set_mean('data', mean)
    transformer.set_mean('data',np.load(mean_filename).mean(1).mean(1))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))
    net.blobs['data'].reshape(1,3,227,227)
    return net,transformer

def aesthetics_evaluate(nt,ft,imgpath):
    net=nt
    transformer=ft
    image = caffe.io.load_image(imgpath)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    output = net.forward()
    output_prob = output['loss1/classifier_cjy'][0]
    return output_prob.argmax()

# imgpath='F:/IMS/photo/KE6G4178.jpg'
# labels = ['low','hight']
# nt,ft=aesthetics_net()
# r=aesthetics_evaluate(nt,ft,imgpath)
# print labels[r]